package com.lanke.clickhouse.datasource.autoconfiguration;

import com.alibaba.druid.pool.DruidDataSource;
import com.lanke.clickhouse.datasource.autoconfiguration.properties.ClickHouseLankeProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Author: lanke
 * @Date: 2021/12/6 17:41
 * @Description: TODO
 **/
@Configuration
@ConditionalOnClass(DruidDataSource.class)
@EnableConfigurationProperties({ClickHouseLankeProperties.class})
@ComponentScan(basePackages = {"com.lanke.clickhouse.datasource"})
public class ClickHouseLankeAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(DruidDataSource.class)
    public DataSource dataSource(ClickHouseLankeProperties clickHouseLancProperties) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setDriverClassName(clickHouseLancProperties.getDriverClassName());
        datasource.setUrl(clickHouseLancProperties.getUrl());
        datasource.setPassword(clickHouseLancProperties.getPassword());
        datasource.setUsername(clickHouseLancProperties.getUsername());
        datasource.setInitialSize(clickHouseLancProperties.getInitialSize());
        datasource.setMaxActive(clickHouseLancProperties.getMaxActive());
        datasource.setMaxWait(clickHouseLancProperties.getMaxWait());
        datasource.setMinIdle(clickHouseLancProperties.getMinIdle());
        datasource.setValidationQuery(clickHouseLancProperties.getValidationQuery());
        return datasource;
    }
}
